﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace TCPClient
{
    public class RichTextBoxPanel
    {

        RichTextBox _richTextBox;
        public RichTextBoxPanel(RichTextBox richTextBox)
        {
            _richTextBox = richTextBox;
            _richTextBox.Multiline = true;  //将Multiline属性设为true，实现显示多行
            _richTextBox.ScrollBars = RichTextBoxScrollBars.Vertical;  //设置ScrollBars属性实现只显示垂直滚动条
        }

        #region 日志记录、支持其他线程访问 
        public delegate void LogAppendDelegate(Color color, string text);
        public delegate void LogClearDelegate();
        /// <summary> 
        /// 追加显示文本 
        /// </summary> 
        /// <param name="color">文本颜色</param> 
        /// <param name="text">显示文本</param> 
        public void LogAppend(Color color, string text)
        {
            if (_richTextBox.Lines.Length > 1000)
            {
                _richTextBox.SelectionStart = 0;
                _richTextBox.SelectionLength = _richTextBox.GetFirstCharIndexFromLine(10) - 1;
                _richTextBox.SelectedText = "";
            }
            _richTextBox.SelectionStart = _richTextBox.Text.Length;
            _richTextBox.SelectionColor = color;
            _richTextBox.AppendText(text);
            _richTextBox.AppendText("\n");
            //滚动到控件光标处
            _richTextBox.ScrollToCaret();
        }
        /// <summary> 
        /// 显示错误日志 
        /// </summary> 
        /// <param name="text"></param> 
        public void LogError(string text)
        {
            if (_richTextBox.Parent != null && _richTextBox.Parent.IsHandleCreated)
            {
                LogAppendDelegate la = new LogAppendDelegate(LogAppend);
                _richTextBox.Invoke(la, Color.Red, DateTime.Now.ToString("HH:mm:ss ") + text);
            }
        }
        /// <summary> 
        /// 显示警告信息 
        /// </summary> 
        /// <param name="text"></param> 
        public void LogWarning(string text)
        {
            if (_richTextBox.Parent != null && _richTextBox.Parent.IsHandleCreated)
            {
                LogAppendDelegate la = new LogAppendDelegate(LogAppend);
                _richTextBox.Invoke(la, Color.Blue, DateTime.Now.ToString("HH:mm:ss ") + text);
            }
        }
        /// <summary> 
        /// 显示信息 
        /// </summary> 
        /// <param name="text"></param> 
        public void LogMessage(string text)
        {
            if (_richTextBox.Parent != null && _richTextBox.Parent.IsHandleCreated)
            {
                LogAppendDelegate la = new LogAppendDelegate(LogAppend);
                _richTextBox.Invoke(la, Color.Black, DateTime.Now.ToString("HH:mm:ss ") + text);
            }
        }

        /// <summary> 
        /// 显示信息 
        /// </summary> 
        /// <param name="text"></param> 
        public void AppendMessage(string text)
        {
            if (_richTextBox.Parent != null && _richTextBox.Parent.IsHandleCreated)
            {
                LogAppendDelegate la = new LogAppendDelegate(LogAppend);
                _richTextBox.Invoke(la, Color.Black, text);
            }
        }

        public void TxtClear()
        {
            if (_richTextBox.InvokeRequired)
            {
                LogClearDelegate lc = new LogClearDelegate(TxtClear);
                {
                    _richTextBox.Invoke(lc);
                }

            }
            else
            {
                _richTextBox.Clear();
            }

        }
        #endregion

    }
}
